package com.lg.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.lg.entity.User;
import com.lg.mapper.JobInfoDao;
import com.lg.mapper.UserMapper;
import com.lg.service.Aservice;
import com.lg.service.Bservice;
import com.lg.service.UserService;
import com.system.base.ApiResult;
import com.system.base.MyTask;
import com.system.constants.QueueConstant;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sky
 * @since 2023-03-11
 */
@SuppressWarnings("rawtypes")
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Autowired
	UserMapper userMapper;
	@Autowired
	UserService userService;
	@Autowired
	JobInfoDao jobInfoDao;
	@Autowired
	Aservice aservice;
	@Autowired
	Bservice bservice;
	
	@Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

	@RequestMapping("query")
	public ApiResult query() throws Exception {
		User user = new User();
		user.setAge(18);
		user.setName("sky");
		user.setAddr("110@163.com");
		// userMapper.insert(user);

		log.info("打印user前，user={}", user);
		
//	    userService.insertUser(user);
	    
	    log.info("打印user后，user={}", user);
	    
	    List<User> userList = userService.listUser();
	    log.info("userList:{}", userList);
	    
	    // jobInfoDao.updateJobInfo();
	    // List<JobInfo> findList = jobInfoDao.findList();
	    
	    MyTask myTask = new MyTask("任务-001", "线程threadPoolTaskExecutor", 1L, 1);
		threadPoolTaskExecutor.execute(myTask);
		
		aservice.test();
		bservice.test();
		
	    log.debug("这是一行debug日志");
	    log.trace("这是一行trace日志");
		return ApiResult.build(200, "查询成功");
	}

	@RequestMapping("delete")
	public ApiResult delete(Integer id) {
		userMapper.deleteById(id);

		List<User> userList = userMapper.listUser();
		userList.forEach(System.out::println);
		return ApiResult.build(200, "删除成功", userList);
	}

	/**
	 * 测试线程池
	 * 
	 */
	@GetMapping("/dealThreadPoolTask")
	public void dealThreadPoolTask() {
		for (int i = 0; i < 1000; i++) {
			MyTask myTask = new MyTask(String.valueOf(i), "线程" + i, 1L, i);
			threadPoolTaskExecutor.execute(myTask);
		}
	}
	
	/**
	 * 测试
	 */
	@RequestMapping("/send")
    public ApiResult sendMessage() {
		String message = "test-";
		for (int i = 100; i < 150; i++) {
			// kafkaTemplate.send("proc-queue-topic", message + i);
			System.out.println(message);
		}
		kafkaTemplate.send(QueueConstant.PROC_PRICE_TOPIC, "00D4DD4919941944E06371001FACA434");
        return ApiResult.build(200, "发送成功！");
    }

}
